package aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: omybug
 * @date: 18/9/29 11:19
 */
public class JdkObjectProxy implements InvocationHandler {

    private Object target;

    /**
     * 如果target类，没有实现接口，那么将无法使用JDK代理
     * @param target
     * @return
     */
    public Object getProxy(Object target){
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("** 开始 **");
        Object object = method.invoke(target, args);
        System.out.println("** 结束 **");
        return object;
    }
}
